/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import BussinessObject.HoadonBO;
import BussinessObject.SanphamBO;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.beans.PropertyVetoException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.NumberFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Iterator;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author ngoctoan
 */
public class HoadonGUI extends javax.swing.JInternalFrame {

    /**
     * Creates new form SanphamGUI
     */
    private DefaultTableModel model = new DefaultTableModel();
    private DefaultTableModel modelDSHoadon = new DefaultTableModel();
    private DefaultTableModel modelCTHD = new DefaultTableModel();
    private ArrayList<SanphamBO> list = new ArrayList<SanphamBO>();
    private int seletedIndex = 0;
    private Locale locale = new Locale("vi", "VN");
    private NumberFormat fmt = NumberFormat.getCurrencyInstance(locale);
    private double giaBan;
    private double tongTien = 0;

    public HoadonGUI() throws PropertyVetoException, SQLException, ParseException {
        initComponents();

        TaoMaHD();
        LoadData();
        model.setColumnIdentifiers(new String[]{"Mã sp", "Tên sp", "ĐVT", "SL", "Đơn giá bán", "Thành tiền"});
        tbSanpham.setModel(model);

        modelDSHoadon.setColumnIdentifiers(new String[]{"Id_Hoadon", "Mã hóa đơn", "Ngày lập", "Tổng tiền", "Ghi chú"});
        tbHoadon.setModel(modelDSHoadon);

        modelCTHD.setColumnIdentifiers(new String[]{"Tên sp", "Số lượng", "Đơn giá", "Thành tiền"});
        tbCthd.setModel(modelCTHD);

        txtMahd.setEditable(false);
        txtNgaylap.setEditable(false);
        txtTensp.setEditable(false);
        txtDvt.setEditable(false);
        txtGiaban.setEditable(false);
        txtTongtien.setEditable(false);
        txtId_Sanpham.setVisible(false);

        LoadHoaDon();

        DefaultTableCellRenderer rightRenderer = new DefaultTableCellRenderer();
        rightRenderer.setHorizontalAlignment(DefaultTableCellRenderer.RIGHT);
        tbHoadon.getColumn("Tổng tiền").setCellRenderer(rightRenderer);
        //tbSanpham.getColumn("SL").setCellRenderer(rightRenderer);
        tbSanpham.getColumn("Thành tiền").setCellRenderer(rightRenderer);
        tbSanpham.getColumn("Đơn giá bán").setCellRenderer(rightRenderer);

        tbHoadon.setRowSelectionInterval(0, 0);
    }

    private int getNumRowRS(ResultSet rs) throws SQLException {
        int row = 0;
        while (rs.next()) {
            row++;
        }
        return row;
    }

    private void LoadHoaDon() throws SQLException, ParseException {
        HoadonBO hdBO = new HoadonBO();
        ResultSet rs = hdBO.LaytatcaHoadon();
        ResultSet rscopy = hdBO.LaytatcaHoadon();
        //System.out.println(getNumRowRS(rscopy));
        modelDSHoadon.setRowCount(getNumRowRS(rscopy));

        int row = 0;
        while (rs.next()) {
            modelDSHoadon.setValueAt(rs.getString("Id_Hoadon"), row, 0);
            modelDSHoadon.setValueAt(rs.getString("MaHd"), row, 1);
            modelDSHoadon.setValueAt(rs.getString("Ngaylap"), row, 2);
            modelDSHoadon.setValueAt(fmt.format(Double.parseDouble(rs.getString("Tongtien"))), row, 3);
            modelDSHoadon.setValueAt(rs.getString("Ghichu"), row, 4);
            row++;
        }
        tbHoadon.setModel(modelDSHoadon);
    }

    private void TinhTongTien() {
        double tongTienLocal = 0;
        for (Iterator<SanphamBO> it = list.iterator(); it.hasNext();) {
            SanphamBO object = it.next();
//            System.out.print(object.Soluong);
//            System.out.print(object.Giaban);
//            System.out.println();
            tongTienLocal += object.Soluong * object.Giaban;
        }
        tongTien = tongTienLocal;
        txtTongtien.setText(fmt.format(tongTienLocal));
    }

    private void TaoMaHD() {
        HoadonBO hdBO = new HoadonBO();
        ResultSet rs = hdBO.LayIDhd();
        int returnId = 0;
        try {
            while (rs.next()) {
                returnId = Integer.parseInt(rs.getString(1)) + 1;
            }
        } catch (SQLException ex) {
            Logger.getLogger(HoadonGUI.class.getName()).log(Level.SEVERE, null, ex);
        }
        txtMahd.setText("HD" + String.format("%05d", returnId));
        String timeStamp = new SimpleDateFormat("dd/MM/yyyy").format(Calendar.getInstance().getTime());
        txtNgaylap.setText(timeStamp);
    }

    private void LoadData() {
        try {
            HoadonBO hdBO = new HoadonBO();
            ResultSet rs = hdBO.LayMasp();
            cboMasp.removeAllItems();
            while (rs.next()) {
                cboMasp.addItem(rs.getString(2));
            }
        } catch (Exception e) {
            System.out.println("Error: " + e);
        }
    }

    public void actionPerformed(ActionEvent e, ArrayList<SanphamBO> list) {

        model.setRowCount(list.size());
        int row = 0;
        for (SanphamBO data : list) {
            model.setValueAt(data.Masp, row, 0);
            model.setValueAt(data.Tensp, row, 1);
            model.setValueAt(data.Donvitinh, row, 2);
            model.setValueAt(data.Soluong, row, 3);
            model.setValueAt(fmt.format(data.Giaban), row, 4);
            model.setValueAt(fmt.format(data.Giaban * data.Soluong), row, 5);
            row++;
        }
        tbSanpham.setModel(model);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jErrorProvider = new javax.swing.JErrorProvider();
        tab2 = new javax.swing.JTabbedPane();
        jTabbedPane2 = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        btnXoa = new javax.swing.JButton();
        txtTensp = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        cboMasp = new javax.swing.JComboBox();
        jLabel7 = new javax.swing.JLabel();
        txtDvt = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        txtGiaban = new javax.swing.JTextField();
        btnLuu = new javax.swing.JButton();
        btnThem = new javax.swing.JButton();
        jLabel9 = new javax.swing.JLabel();
        txtSoluong = new javax.swing.JTextField();
        txtId_Sanpham = new javax.swing.JTextField();
        btnLaphd = new javax.swing.JButton();
        btnThoat = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbSanpham = new javax.swing.JTable();
        jPanel4 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        txtMahd = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        txtNgaylap = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        txtTongtien = new javax.swing.JTextField();
        jScrollPane2 = new javax.swing.JScrollPane();
        txtGhichu = new javax.swing.JTextArea();
        jLabel6 = new javax.swing.JLabel();
        btnHelp = new javax.swing.JButton();
        jTabbedPane3 = new javax.swing.JTabbedPane();
        jPanel5 = new javax.swing.JPanel();
        jPanel6 = new javax.swing.JPanel();
        jScrollPane3 = new javax.swing.JScrollPane();
        tbHoadon = new javax.swing.JTable();
        jPanel7 = new javax.swing.JPanel();
        jScrollPane4 = new javax.swing.JScrollPane();
        tbCthd = new javax.swing.JTable();
        btnThoat1 = new javax.swing.JButton();
        btnHelp1 = new javax.swing.JButton();
        jPanel8 = new javax.swing.JPanel();
        txtNgaylap1 = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();
        txtMahd1 = new javax.swing.JTextField();
        jLabel11 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        txtTongtien1 = new javax.swing.JTextField();
        jScrollPane5 = new javax.swing.JScrollPane();
        txtGhichu1 = new javax.swing.JTextArea();
        jLabel13 = new javax.swing.JLabel();
        txtId_Hoadon = new javax.swing.JTextField();

        setClosable(true);
        setTitle("QUẢN LÝ HÓA ĐƠN BÁN HÀNG");
        setPreferredSize(new java.awt.Dimension(900, 637));

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)), "Thông tin chi tiết hóa đơn"));

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel1.setText("Mã sản phẩm");

        btnXoa.setIcon(new javax.swing.ImageIcon("D:\\supermarket-management-system\\src\\resource\\delete.png")); // NOI18N
        btnXoa.setText("Xóa");
        btnXoa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnXoaActionPerformed(evt);
            }
        });

        txtTensp.setPreferredSize(new java.awt.Dimension(10, 25));

        jLabel5.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel5.setText("Tên sản phẩm");

        cboMasp.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        cboMasp.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cboMaspItemStateChanged(evt);
            }
        });

        jLabel7.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel7.setText("ĐVT");

        txtDvt.setPreferredSize(new java.awt.Dimension(10, 25));

        jLabel8.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel8.setText("Đơn giá bán");

        txtGiaban.setPreferredSize(new java.awt.Dimension(10, 25));

        btnLuu.setIcon(new javax.swing.ImageIcon("D:\\supermarket-management-system\\src\\resource\\save.png")); // NOI18N
        btnLuu.setText("Lưu");
        btnLuu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLuuActionPerformed(evt);
            }
        });

        btnThem.setIcon(new javax.swing.ImageIcon("D:\\supermarket-management-system\\src\\resource\\add.png")); // NOI18N
        btnThem.setText("Thêm");
        btnThem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnThemActionPerformed(evt);
            }
        });

        jLabel9.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel9.setText("Số lượng");

        txtSoluong.setPreferredSize(new java.awt.Dimension(10, 25));
        txtSoluong.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtSoluongKeyTyped(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(txtId_Sanpham, javax.swing.GroupLayout.DEFAULT_SIZE, 19, Short.MAX_VALUE)
                        .addGap(18, 18, 18)
                        .addComponent(btnThem, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(btnLuu, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(btnXoa, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel9)
                            .addComponent(jLabel8)
                            .addComponent(jLabel5)
                            .addComponent(jLabel1))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(cboMasp, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(txtTensp, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(txtGiaban, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(txtSoluong, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(24, 24, 24)
                                .addComponent(jLabel7)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txtDvt, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cboMasp, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtTensp, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtDvt, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel9, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtSoluong, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtGiaban, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnXoa, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnLuu, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnThem, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtId_Sanpham, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(21, Short.MAX_VALUE))
        );

        btnLaphd.setIcon(new javax.swing.ImageIcon("D:\\supermarket-management-system\\src\\resource\\hd.png")); // NOI18N
        btnLaphd.setText("Lập hóa đơn");
        btnLaphd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLaphdActionPerformed(evt);
            }
        });

        btnThoat.setIcon(new javax.swing.ImageIcon("D:\\supermarket-management-system\\src\\resource\\quit.png")); // NOI18N
        btnThoat.setText("Thoát");
        btnThoat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnThoatActionPerformed(evt);
            }
        });

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0))), "Chi tiết hóa đơn"));

        tbSanpham.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        tbSanpham.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tbSanpham.setRowHeight(25);
        tbSanpham.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tbSanphamMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tbSanpham);

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                .addContainerGap())
        );

        jPanel4.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel2.setText("Mã hóa đơn");

        txtMahd.setPreferredSize(new java.awt.Dimension(10, 25));

        jLabel4.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel4.setText("Ngày lập");

        txtNgaylap.setPreferredSize(new java.awt.Dimension(10, 25));

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel3.setText("Tổng tiền");

        txtTongtien.setPreferredSize(new java.awt.Dimension(10, 25));

        txtGhichu.setColumns(20);
        txtGhichu.setRows(20);
        jScrollPane2.setViewportView(txtGhichu);

        jLabel6.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel6.setText("Ghi chú");

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addComponent(jLabel2)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(txtMahd, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(txtNgaylap, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 185, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(txtTongtien, javax.swing.GroupLayout.PREFERRED_SIZE, 291, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 87, Short.MAX_VALUE)
                .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(13, 13, 13)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 300, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addGap(47, 47, 47)
                        .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtMahd, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtNgaylap, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtTongtien, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        btnHelp.setIcon(new javax.swing.ImageIcon("D:\\supermarket-management-system\\src\\resource\\help.png")); // NOI18N
        btnHelp.setText("Giúp đở");
        btnHelp.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnHelpActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(btnLaphd, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(btnHelp, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(btnThoat, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnHelp, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(btnThoat, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(btnLaphd, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(32, 32, 32))
        );

        jTabbedPane2.addTab("", jPanel1);

        tab2.addTab("Lập hóa đơn bán hàng", jTabbedPane2);

        jPanel6.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0))), "Hóa đơn đã lập"));

        tbHoadon.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        tbHoadon.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tbHoadon.setRowHeight(25);
        tbHoadon.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tbHoadonMouseClicked(evt);
            }
        });
        jScrollPane3.setViewportView(tbHoadon);

        javax.swing.GroupLayout jPanel6Layout = new javax.swing.GroupLayout(jPanel6);
        jPanel6.setLayout(jPanel6Layout);
        jPanel6Layout.setHorizontalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel6Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 474, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel6Layout.setVerticalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel6Layout.createSequentialGroup()
                .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 228, Short.MAX_VALUE)
                .addContainerGap())
        );

        jPanel7.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0))), "Chi tiết hóa đơn"));

        tbCthd.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        tbCthd.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tbCthd.setRowHeight(25);
        jScrollPane4.setViewportView(tbCthd);

        javax.swing.GroupLayout jPanel7Layout = new javax.swing.GroupLayout(jPanel7);
        jPanel7.setLayout(jPanel7Layout);
        jPanel7Layout.setHorizontalGroup(
            jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel7Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane4)
                .addContainerGap())
        );
        jPanel7Layout.setVerticalGroup(
            jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel7Layout.createSequentialGroup()
                .addComponent(jScrollPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 168, Short.MAX_VALUE)
                .addContainerGap())
        );

        btnThoat1.setIcon(new javax.swing.ImageIcon("D:\\supermarket-management-system\\src\\resource\\quit.png")); // NOI18N
        btnThoat1.setText("Thoát");
        btnThoat1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnThoat1ActionPerformed(evt);
            }
        });

        btnHelp1.setIcon(new javax.swing.ImageIcon("D:\\supermarket-management-system\\src\\resource\\help.png")); // NOI18N
        btnHelp1.setText("Giúp đở");
        btnHelp1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnHelp1ActionPerformed(evt);
            }
        });

        jPanel8.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0))), "Chi tiết hóa đơn"));

        txtNgaylap1.setPreferredSize(new java.awt.Dimension(10, 25));

        jLabel10.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel10.setText("Mã hóa đơn");

        txtMahd1.setPreferredSize(new java.awt.Dimension(10, 25));

        jLabel11.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel11.setText("Ngày lập");

        jLabel12.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel12.setText("Tổng tiền");

        txtTongtien1.setPreferredSize(new java.awt.Dimension(10, 25));

        txtGhichu1.setColumns(1);
        txtGhichu1.setRows(20);
        jScrollPane5.setViewportView(txtGhichu1);

        jLabel13.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel13.setText("Ghi chú");

        txtId_Hoadon.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtId_HoadonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel8Layout = new javax.swing.GroupLayout(jPanel8);
        jPanel8.setLayout(jPanel8Layout);
        jPanel8Layout.setHorizontalGroup(
            jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel8Layout.createSequentialGroup()
                .addGap(34, 34, 34)
                .addGroup(jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel12)
                    .addComponent(jLabel10)
                    .addComponent(jLabel11)
                    .addComponent(jLabel13, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jScrollPane5)
                    .addComponent(txtTongtien1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(jPanel8Layout.createSequentialGroup()
                        .addGroup(jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(txtMahd1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 150, Short.MAX_VALUE)
                            .addComponent(txtNgaylap1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(txtId_Hoadon, javax.swing.GroupLayout.DEFAULT_SIZE, 25, Short.MAX_VALUE)))
                .addContainerGap(36, Short.MAX_VALUE))
        );
        jPanel8Layout.setVerticalGroup(
            jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel8Layout.createSequentialGroup()
                .addGroup(jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtMahd1, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel10, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtId_Hoadon, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtNgaylap1, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel11, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel12, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtTongtien1, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel8Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jScrollPane5, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel8Layout.createSequentialGroup()
                        .addGap(37, 37, 37)
                        .addComponent(jLabel13, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(0, 0, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel5Layout.createSequentialGroup()
                        .addGap(14, 14, 14)
                        .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jPanel7, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel5Layout.createSequentialGroup()
                                .addComponent(jPanel6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 15, Short.MAX_VALUE)
                                .addComponent(jPanel8, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel5Layout.createSequentialGroup()
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btnHelp1, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(btnThoat1, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel8, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnHelp1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnThoat1, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(44, 44, 44))
        );

        jPanel6.getAccessibleContext().setAccessibleName("");
        jPanel8.getAccessibleContext().setAccessibleName("Thông tin hóa đơn");

        jTabbedPane3.addTab("", jPanel5);

        tab2.addTab("Danh sách hóa đơn đã lập", jTabbedPane3);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(tab2)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(tab2)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cboMaspItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cboMaspItemStateChanged
        try {
            // TODO add your handling code here:
            jErrorProvider.clear();
            HoadonBO hdBO = new HoadonBO();
            ResultSet rs = hdBO.LayCtsp(cboMasp.getSelectedItem());

            while (rs.next()) {
                txtId_Sanpham.setText(rs.getString("ID_Sanpham"));
                txtTensp.setText(rs.getString("Tensp"));
                txtDvt.setText(rs.getString("Donvitinh"));
                giaBan = Double.parseDouble(rs.getString("Giaban"));
                txtGiaban.setText(fmt.format(Double.parseDouble(rs.getString("Giaban"))));
                txtSoluong.setText("");
            }
        } catch (SQLException ex) {
            Logger.getLogger(HoadonGUI.class.getName()).log(Level.SEVERE, null, ex);
        }

    }//GEN-LAST:event_cboMaspItemStateChanged

    private void btnThemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnThemActionPerformed
        // TODO add your handling code here:
        txtSoluong.setText("");
        cboMasp.setSelectedIndex(0);
    }//GEN-LAST:event_btnThemActionPerformed

    private void txtSoluongKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtSoluongKeyTyped
        char c = evt.getKeyChar();
        if (!((c >= '0') && (c <= '9') || (c == KeyEvent.VK_BACK_SPACE) || (c == KeyEvent.VK_DELETE) || (c == KeyEvent.VK_ENTER) || (c == KeyEvent.VK_PERIOD))) {
            evt.consume();
        }
    }//GEN-LAST:event_txtSoluongKeyTyped

    private boolean VadidateHoadon() {
        if (tbSanpham.getRowCount() == 0) {
            jErrorProvider.setError(btnLaphd, "Phải nhập chi tiết hóa đơnn");
            return false;
        }
        return true;
    }

    private boolean VadidateCTHD() {
        if (txtSoluong.getText().trim().equals("")) {
            jErrorProvider.setError(txtSoluong, "Phải nhập chi tiết phiếu nhập");
            return false;
        }
        return true;
    }

    private void btnLuuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLuuActionPerformed
        // TODO add your handling code here:
        if (VadidateCTHD()) {
            int ID_Sanpham = Integer.parseInt(txtId_Sanpham.getText());
            String Masp = (String) cboMasp.getSelectedItem();
            String Tensp = txtTensp.getText();
            String Donvitinh = txtDvt.getText();
            //double Giaban = Double.parseDouble(txtGiaban.getText().trim());
            int Soluong = Integer.parseInt(txtSoluong.getText().trim());

            for (Iterator<SanphamBO> it = list.iterator(); it.hasNext();) {
                SanphamBO object = it.next();
                if (ID_Sanpham == object.ID_Sanpham) {
                    list.get(seletedIndex).Soluong = Soluong;
                    list.get(seletedIndex).Giaban = giaBan;
                    actionPerformed(evt, list);
                    cboMasp.setSelectedIndex(0);
                    TinhTongTien();
                    return;
                }
            }
            list.add(new SanphamBO(ID_Sanpham, Masp, Tensp, Donvitinh, giaBan, Soluong));
            actionPerformed(evt, list);
            TinhTongTien();
        }
    }//GEN-LAST:event_btnLuuActionPerformed

    private void tbSanphamMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbSanphamMouseClicked
        // TODO add your handling code here:
        cboMasp.setSelectedItem(tbSanpham.getValueAt(tbSanpham.getSelectedRow(), 0).toString());
        txtSoluong.setText(tbSanpham.getValueAt(tbSanpham.getSelectedRow(), 3).toString());
        seletedIndex = tbSanpham.getSelectedRow();
    }//GEN-LAST:event_tbSanphamMouseClicked

    private void btnXoaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnXoaActionPerformed
        // TODO add your handling code here:
        int dialogButton = JOptionPane.YES_NO_OPTION;
        int dialogResult = JOptionPane.showConfirmDialog(this, "Bạn có chắc chắn xóa chi tiết phiếu nhập?", "Xóa chi tiết phiếu nhập", dialogButton);
        if (dialogResult == 0) {
            list.remove(seletedIndex);
            actionPerformed(evt, list);
            TinhTongTien();
            //JOptionPane.showMessageDialog(null, "Xóa thành công chi tiết phiếu nhập!", "Xóa chi tiết phiếu nhập", JOptionPane.NO_OPTION);
        }
    }//GEN-LAST:event_btnXoaActionPerformed

    private void btnLaphdActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLaphdActionPerformed
        // TODO add your handling code here:
        jErrorProvider.clear();
        if (VadidateHoadon()) {
            HoadonBO hdBO = new HoadonBO();
            String Mahd = txtMahd.getText().trim();
            //Double Tongtien = Double.parseDouble(txtTongtien.getText().trim());
            String Ghichu = txtGhichu.getText().trim();
            int Id_Nhapxuat = hdBO.ThemHoadon(Mahd, tongTien, Ghichu);

            for (SanphamBO data : list) {
                int Id_Sanpham = data.ID_Sanpham;
                System.out.println(Id_Sanpham);
                int Soluong = data.Soluong;
                double Giaban = data.Giaban;
                double Thanhtien = data.Soluong * data.Giaban;
                hdBO.ThemCthd(Id_Nhapxuat, Id_Sanpham, Soluong, Giaban, Thanhtien);
                hdBO.CapNhatSPNhapHang(Id_Sanpham, Soluong);
            }

            list.clear();
            actionPerformed(evt, list);
            TinhTongTien();
            tab2.setSelectedIndex(1);
            TaoMaHD();
        }
    }//GEN-LAST:event_btnLaphdActionPerformed

    private void btnThoatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnThoatActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_btnThoatActionPerformed

    private void btnHelpActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnHelpActionPerformed
        // TODO add your handling code here:
        JOptionPane.showMessageDialog(null, "Thêm chi tiết hóa đơn và chọn nút lập hóa đơn", "Hướng dẫn", JOptionPane.NO_OPTION);
    }//GEN-LAST:event_btnHelpActionPerformed

    private void btnThoat1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnThoat1ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_btnThoat1ActionPerformed

    private void btnHelp1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnHelp1ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_btnHelp1ActionPerformed

    private void tbHoadonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbHoadonMouseClicked
        // TODO add your handling code here:
        Object Id_Hoadon = tbHoadon.getValueAt(tbHoadon.getSelectedRow(), 0);
        txtMahd1.setText(tbHoadon.getValueAt(tbHoadon.getSelectedRow(), 1).toString());
        txtNgaylap1.setText(tbHoadon.getValueAt(tbHoadon.getSelectedRow(), 2).toString());
        txtTongtien1.setText(tbHoadon.getValueAt(tbHoadon.getSelectedRow(), 3).toString());
        txtGhichu1.setText(tbHoadon.getValueAt(tbHoadon.getSelectedRow(), 4).toString());

        HoadonBO hdBO = new HoadonBO();
        ResultSet rs = hdBO.LayCTHD(Id_Hoadon);
        ResultSet rscopy = hdBO.LayCTHD(Id_Hoadon);
        try {
            modelCTHD.setRowCount(getNumRowRS(rscopy));
        } catch (SQLException ex) {
            Logger.getLogger(HoadonGUI.class.getName()).log(Level.SEVERE, null, ex);
        }

        int row = 0;
        try {
            while (rs.next()) {
                modelCTHD.setValueAt(rs.getString("Tensp"), row, 0);
                modelCTHD.setValueAt(rs.getString("Soluong"), row, 1);
                modelCTHD.setValueAt(fmt.format(Double.parseDouble(rs.getString("Dongia"))), row, 2);
                modelCTHD.setValueAt(fmt.format(Double.parseDouble(rs.getString("Thanhtien"))), row, 3);

                row++;
            }
        } catch (SQLException ex) {
            Logger.getLogger(HoadonGUI.class.getName()).log(Level.SEVERE, null, ex);
        }
        tbCthd.setModel(modelCTHD);
    }//GEN-LAST:event_tbHoadonMouseClicked

    private void txtId_HoadonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtId_HoadonActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtId_HoadonActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnHelp;
    private javax.swing.JButton btnHelp1;
    private javax.swing.JButton btnLaphd;
    private javax.swing.JButton btnLuu;
    private javax.swing.JButton btnThem;
    private javax.swing.JButton btnThoat;
    private javax.swing.JButton btnThoat1;
    private javax.swing.JButton btnXoa;
    private javax.swing.JComboBox cboMasp;
    private javax.swing.JErrorProvider jErrorProvider;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JPanel jPanel7;
    private javax.swing.JPanel jPanel8;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JScrollPane jScrollPane5;
    private javax.swing.JTabbedPane jTabbedPane2;
    private javax.swing.JTabbedPane jTabbedPane3;
    private javax.swing.JTabbedPane tab2;
    private javax.swing.JTable tbCthd;
    private javax.swing.JTable tbHoadon;
    private javax.swing.JTable tbSanpham;
    private javax.swing.JTextField txtDvt;
    private javax.swing.JTextArea txtGhichu;
    private javax.swing.JTextArea txtGhichu1;
    private javax.swing.JTextField txtGiaban;
    private javax.swing.JTextField txtId_Hoadon;
    private javax.swing.JTextField txtId_Sanpham;
    private javax.swing.JTextField txtMahd;
    private javax.swing.JTextField txtMahd1;
    private javax.swing.JTextField txtNgaylap;
    private javax.swing.JTextField txtNgaylap1;
    private javax.swing.JTextField txtSoluong;
    private javax.swing.JTextField txtTensp;
    private javax.swing.JTextField txtTongtien;
    private javax.swing.JTextField txtTongtien1;
    // End of variables declaration//GEN-END:variables
}
